package com.aji.utills;

import com.aji.entity.*;
import com.csvreader.CsvWriter;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

public class CsvUtills {
    public static void wCsv(String file, List<ElementTrain> elementTrainList) throws IOException {

        CsvWriter csvWriter = new CsvWriter(file
                , ',', Charset.forName("GBK"));
        String title = "Li\tBe\tB\tNa\tMg\tAl\tSi\tP\tS\tK\tCa\tSc\tTi\tV\tCr\tMn\tFe\tCo\tNi\tCu\tZn\tGa\tGe\tAs\tSe\tRb\tSr\tY\tZr\tNb\tMo\tRu\tRh\tPd\tAg\tCd\tIn\tSn\tSb\tTe\tI\tCs\tBa\tLa\tCe\tPr\tNd\tSm\tEu\tGd\tTb\tDy\tHo\tEr\tTm\tYb\tLu\tHf\tTa\tW\tRe\tOs\tIr\tPt\tAu\tHg\tTl\tBi\tPb\tTh\tU\tlabel";
        String[] titles = title.split("\t");
        csvWriter.writeRecord(titles);
        for (ElementTrain elementTrain : elementTrainList) {
            String[] element = elementTrain.getElement().split("\t");
            String ores1Id = String.valueOf(elementTrain.getOres1Id());
            String[] elementRow = new String[element.length + 1];
            System.arraycopy(element, 0, elementRow, 0, element.length);
            elementRow[elementRow.length - 1] = ores1Id;
            csvWriter.writeRecord(elementRow);

        }
        csvWriter.close();
    }

    public static void wCsvTest(String file, List<ElementTest> elementTests) throws IOException {

        CsvWriter csvWriter = new CsvWriter(file
                , ',', Charset.forName("GBK"));
        String title = "contents\tLi\tBe\tB\tNa\tMg\tAl\tSi\tP\tS\tK\tCa\tSc\tTi\tV\tCr\tMn\tFe\tCo\tNi\tCu\tZn\tGa\tGe\tAs\tSe\tRb\tSr\tY\tZr\tNb\tMo\tRu\tRh\tPd\tAg\tCd\tIn\tSn\tSb\tTe\tI\tCs\tBa\tLa\tCe\tPr\tNd\tSm\tEu\tGd\tTb\tDy\tHo\tEr\tTm\tYb\tLu\tHf\tTa\tW\tRe\tOs\tIr\tPt\tAu\tHg\tTl\tBi\tPb\tTh\tU";
        String[] titles = title.split("\t");
        csvWriter.writeRecord(titles);
        for (ElementTest elementTest : elementTests) {
            String[] element = elementTest.getElement().split("\t");
            String[] elementRow = new String[element.length + 1];
            System.arraycopy(element, 0, elementRow, 1, element.length);
            elementRow[0] = elementTest.getContents();
            csvWriter.writeRecord(elementRow);

        }
        csvWriter.close();
    }

    public static void wCsvIso(String file, List<IsotopeTrain> isotopeTrainList) throws IOException {

        CsvWriter csvWriter = new CsvWriter(file
                , ',', Charset.forName("GBK"));
        String title = "a\tlabel";
        String[] titles = title.split("\t");
        csvWriter.writeRecord(titles);
        for (IsotopeTrain isotopeTrain : isotopeTrainList) {
            String[] isotope = isotopeTrain.getA().split("\t");
            String ores1Id = String.valueOf(isotopeTrain.getOres1Id());
            String[] isotopeRow = new String[isotope.length + 1];
            System.arraycopy(isotope, 0, isotopeRow, 0, isotope.length);
            isotopeRow[isotopeRow.length - 1] = ores1Id;
            csvWriter.writeRecord(isotopeRow);

        }
        csvWriter.close();
    }

    public static void wCsvIsoTest(String file, List<IsotopeTest> isotopeTests) throws IOException {

        CsvWriter csvWriter = new CsvWriter(file
                , ',', Charset.forName("GBK"));
        String title = "contents\ta\t";
        String[] titles = title.split("\t");
        csvWriter.writeRecord(titles);
        for (IsotopeTest isotopeTest : isotopeTests) {
            String[] isotope = isotopeTest.getA().split("\t");
            String[] isotopeRow = new String[isotope.length + 1];
            System.arraycopy(isotope, 0, isotopeRow, 1, isotope.length);
            isotopeRow[0] = isotopeTest.getContents();
            csvWriter.writeRecord(isotopeRow);

        }
        csvWriter.close();
    }
}

